package erwiki.ui.app.internal;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

/**
 * Utility class to handle image resources.</br>
 * Cannot construct an ImageResource. Use static methods only.
 */
public class ImageResource {

	// the image registry
	private static final ImageRegistry imageRegistry;

	// map of image descriptors since these
	// will be lost by the image registry
	private static final Map<String, ImageDescriptor> imageDescriptors;

	private static final String URL_PREFIX = "$nl$/icons/"; //$NON-NLS-1$

	// Object's images.
	private static final String URL_OBJ = URL_PREFIX + "obj16/"; //$NON-NLS-1$

	// --- constants for images ---

	public static final String IMG_DOOR = "IMG_DOOR";
	public static final String IMG_DOOR_IN = "IMG_DOOR_IN";
	public static final String IMG_DOOR_OPEN = "IMG_DOOR_OPEN";
	public static final String IMG_DOOR_OUT = "IMG_DOOR_OUT";

	public static final String IMG_USER = "IMG_USER";
	public static final String IMG_USER_UNKNOWN = "IMG_USER_UNKNOWN";

	public static final String IMG_EDIT = "IMG_EDIT";

	private static Bundle bundle;
	
	static {
		bundle = FrameworkUtil.getBundle(ImageResource.class);
	}

	// == CODE ================================================================

	// Initialization
	static {
		imageRegistry = new ImageRegistry();
		imageDescriptors = new HashMap<>();

		// Изображения - объекты
		registerImage(IMG_DOOR, URL_OBJ + "door.png"); //$NON-NLS-1$
		registerImage(IMG_DOOR_IN, URL_OBJ + "door_in.png"); //$NON-NLS-1$
		registerImage(IMG_DOOR_OPEN, URL_OBJ + "door_open.png"); //$NON-NLS-1$
		registerImage(IMG_DOOR_OUT, URL_OBJ + "door_out.png"); //$NON-NLS-1$

		registerImage(IMG_USER, URL_OBJ + "user.png"); //$NON-NLS-1$
		registerImage(IMG_USER_UNKNOWN, URL_OBJ + "user_unknown.png"); //$NON-NLS-1$

		registerImage(IMG_EDIT, URL_PREFIX + "edit_action.gif"); //$NON-NLS-1$
	}

	/**
	 * Register an image with the registry.
	 * 
	 * @param key
	 *            java.lang.String
	 * @param partialURL
	 *            java.lang.String
	 */
	private static void registerImage(String key, String partialURL) {
		try {
			URL url = FileLocator.find(bundle, new Path(partialURL), null);
			ImageDescriptor id = ImageDescriptor.createFromURL(url);
			imageRegistry.put(key, id);
			imageDescriptors.put(key, id);
		} catch (Exception e) {
			// TODO:
			// DbCoreLog.log(IStatus.WARNING, 0, "Error registering image " + key + " from " + partialURL, e);
		}
	}

	/**
	 * Return the image with the given key.
	 * 
	 * @param key
	 *            java.lang.String
	 * @return org.eclipse.swt.graphics.Image
	 */
	public static Image getImage(String key) {
		return imageRegistry.get(key);
	}

	/**
	 * Return the image descriptor with the given key.
	 * 
	 * @param key
	 *            java.lang.String
	 * @return org.eclipse.jface.resource.ImageDescriptor
	 */
	public static ImageDescriptor getImageDescriptor(String key) {
		return imageDescriptors.get(key);
	}
}
